namespace :wp do
  desc '导出没有时间的数据'
  task :no_time_date => :environment do
    client = client_connection
    time_hash={}
    client.query('select * from news').each do |row|
      time_hash[row['id']]=row['addtime']
    end
    news = News.all
    #p time_hash[34118]
    news.each do |n|
     if n.update_attributes(pubtime:time_hash[n.id],created_at:time_hash[n.id])
      p "#{n.id}==>true"
     else
      p 'error'
     end

    end


  end

    desc '时间 nil 的数据'
  task :nil_create_date => :environment do
    
    news = News.where("created_at is null")
    #p time_hash[34118]
    news.each do |n|
     if n.update_attributes(created_at:Time.now)
      p "#{n.id}==>true"
     else
      p 'error'
     end

    end


  end


  private
  def client_connection
    Mysql2::Client.new(
        :host => "localhost",
        :username => "root",
        :password => "xb_2014",
        :database => "old_news")
  end
end


